package org.dromara.chicken.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.chicken.domain.ChickenHouse;
import org.dromara.chicken.mapper.ChickenHouseMapper;
import org.dromara.chicken.service.IChickenHouseService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 鸡舍管理Service业务层处理
 *
 * @author Mr Lei
 * @date 2024-06-04
 */
@RequiredArgsConstructor
@Service
public class ChickenHouseServiceImpl implements IChickenHouseService {

    private final ChickenHouseMapper baseMapper;

    /**
     * 查询鸡舍管理
     */
    @Override
    public ChickenHouse queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询鸡舍管理列表
     */
    @Override
    public TableDataInfo<ChickenHouse> queryPageList(ChickenHouse bo, PageQuery pageQuery) {
        Page<ChickenHouse> result = baseMapper.queryPageList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询鸡舍管理列表
     */
    @Override
    public List<ChickenHouse> queryList(ChickenHouse bo) {
        LambdaQueryWrapper<ChickenHouse> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ChickenHouse> buildQueryWrapper(ChickenHouse bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ChickenHouse> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), ChickenHouse::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getHouseNum()), ChickenHouse::getHouseNum, bo.getHouseNum());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), ChickenHouse::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增鸡舍管理
     */
    @Override
    public Boolean insertByBo(ChickenHouse bo) {
        ChickenHouse add = MapstructUtils.convert(bo, ChickenHouse.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改鸡舍管理
     */
    @Override
    public Boolean updateByBo(ChickenHouse bo) {
        validEntityBeforeSave(bo);
        return baseMapper.updateById(bo) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ChickenHouse entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除鸡舍管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
